<?php


/**
* CakePHP Routines Plugin
* http://cake-routines-plugin.googlecode.com
*
* @package cake.plugins.routines
* @subpackage cake.plugins.routines.models.behaviors
* @author Ivan Chistyakov <zetruger@gmail.com>
* @version $Id: associable.php 23 2010-04-28 19:43:30Z zetruger@gmail.com $
* @link $HeadURL: http://cake-routines-plugin.googlecode.com/svn/trunk/models/behaviors/associable.php $
* @license MIT License (http://opensource.org/licenses/mit-license.php)
*/


class AssociableBehavior extends ModelBehavior {

function isTree(&$Model) {
	return $Model->hasField($this->getParentKey($Model));
}

function getTreeSettings(&$Model) {
return FALSE;
}

function getParentKey(&$Model) {
	$settings = $this->getTreeSettings($Model);
	return isset($settings['parent']) ? $settings['parent'] : FALSE;
}

function isParentKey(&$Model, $fieldName) {
	return $Model->hasField($fieldName) && ($fieldName === $this->getParentKey($Model));
}

function getParentId(&$Model, $id) {
	$parentKey = $this->getParentKey($Model);
	return $Model->hasField($parentKey) ? $Model->read($parentKey, $id) : NULL;
}

function isRubric(&$Model) {
	return $this->hasItem($Model);
}

function hasRubric(&$Model) {
	return $Model->hasField($this->getRubricForeignKey($Model));
}

function getRubricAlias(&$Model) {
	foreach ($Model->belongsTo as $alias => $association) {
		if ($alias === $Model->alias . 'Rubric') {
			return $alias;
		} elseif ($association['className'] === $Model->name . 'Rubric') {
			return $alias;
		} elseif (isset($association['isRubric']) && (bool)$association['isRubric']) {
			return $alias;
		}
	}
	return FALSE;
}

function getRubricAssociation(&$Model) {
	$alias = $this->getRubricAlias($Model);
	return isset($Model->belongsTo[$alias]) ? $Model->belongsTo[$alias] : FALSE;
}

function getRubricClassName(&$Model) {
	$association = $this->getRubricAssociation($Model);
	return isset($association['className']) ? $association['className'] : FALSE;
}

function getRubricModel(&$Model) {
	$vars = get_object_vars($Model);
	$alias = $this->getRubricAlias($Model);
	return isset($vars[$alias]) ? $vars[$alias] : FALSE;
}

function getRubricForeignKey(&$Model) {
	$association = $this->getRubricAssociation($Model);
	return isset($association['foreignKey']) ? $association['foreignKey'] : FALSE;
}

function isRubricForeignKey(&$Model, $fieldName) {
	return $Model->hasField($fieldName) && ($fieldName === $this->getRubricForeignKey($Model));
}

function getRubricId(&$Model, $id) {
	$foreignKey = $this->getRubricForeignKey($Model);
	return $Model->hasField($foreignKey) ? $Model->read($foreignKey, $id) : NULL;
}

function isItem(&$Model) {
	return $this->hasRubric($Model);
}

function hasItem(&$Model) {
	$ItemModel = &$this->getItemModel($Model);
	return is_object($ItemModel) ? $this->hasRubric($ItemModel) : FALSE;
}

function getItemAlias(&$Model) {
	foreach ($Model->hasMany as $alias => $association) {
		if ($alias . 'Rubric' === $Model->alias) {
			return $alias;
		} elseif ($association['className'] . 'Rubric' === $Model->name) {
			return $alias;
		} elseif (isset($association['isItem']) && (bool)$association['isItem']) {
			return $alias;
		}
	}
	return FALSE;
}

function getItemAssociation(&$Model) {
	$alias = $this->getItemAlias($Model);
	return isset($Model->hasMany[$alias]) ? $Model->hasMany[$alias] : FALSE;
}

function getItemClassName(&$Model) {
	$association = $this->getItemAssociation($Model);
	return isset($association['className']) ? $association['className'] : FALSE;
}

function getItemModel(&$Model) {
	$vars = get_object_vars($Model);
	$alias = $this->getItemAlias($Model);
	return isset($vars[$alias]) ? $vars[$alias] : FALSE;
}

function isAttachment(&$Model) {
	return $Model->hasField($this->getAttachmentModelForeignKey($Model));
}

function getAttachmentModelForeignKey(&$Model) {
	return array('modelKey' => 'model', 'foreignKey' => 'foreign_key');
}

function getAttachmentValues(&$Model, $id) {
	$modelForeignKey = $this->getAttachmentModelForeignKey($Model);
	return $Model->hasField($modelForeignKey) ? $Model->read($modelForeignKey, $id) : NULL;
}

function hasForeign(&$Model, $fieldName) {
	return $Model->isForeignKey($fieldName) && $Model->hasField($fieldName);
}

function getForeignAlias(&$Model, $fieldName) {
	foreach ($Model->belongsTo as $alias => $association) {
		if ($association['foreignKey'] === $fieldName) {
			return $alias;
		}
	}
	return FALSE;
}

function getForeignAssociation(&$Model, $fieldName) {
	$alias = $this->getForeignAlias($Model, $fieldName);
	return isset($Model->belongsTo[$alias]) ? $Model->belongsTo[$alias] : FALSE;
}

function getForeignClassName(&$Model, $fieldName) {
	$association = $this->getForeignAssociation($Model, $fieldName);
	return isset($association['className']) ? $association['className'] : FALSE;
}

function getForeignModel(&$Model, $fieldName) {
	$vars = get_object_vars($Model);
	$alias = $this->getForeignAlias($Model, $fieldName);
	return isset($vars[$alias]) ? $vars[$alias] : FALSE;
}

}


?>